package ru.spb.osll.myfit.server.handler;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.server.parser.PageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;

/**
 * Created by IntelliJ IDEA.
 * User: Vassar
 * Date: 26.05.2011
 * Time: 20:36:04
 * To change this template use File | Settings | File Templates.
 */
public class ParentVariablesCreatorTest {
  private TestParentVariablesCreator creator;
  private PageLoader loader;
  private String CONTENT = "content.txt";

  @Before
  public void setUp() throws SecurityException, NoSuchMethodException {
      loader = createMock(PageLoader.class);
      creator = new TestParentVariablesCreator(loader);
  }

  @Test
  public void getListPathTest()throws IOException {
    String root = "ROOT";
    String fileName = root + File.separator + "MyTest" + File.separator + "MyTest2" + File.separator +
            "MyTest3" + File.separator + CONTENT;
    TestParentVariablesCreator parentVariablesCreator = new TestParentVariablesCreator(new PageLoader());
    List<String> list = new ArrayList<String>();

    parentVariablesCreator.getListPath(list, root, fileName);
    assertEquals(2, list.size());
    assertEquals(list.get(0), root + File.separator + "MyTest" + File.separator + CONTENT);
    assertEquals(list.get(1), root + File.separator + "MyTest" + File.separator + "MyTest2" + File.separator + CONTENT);
  }

  @Test
  public void getParentVariablesTest()throws IOException {
    String root = "ROOT";
    String fileName = root + File.separator + "MyTest" + File.separator + "MyTest2" + File.separator +
            "MyTest3" + File.separator + CONTENT;

    List<String> list = new ArrayList<String>();
    list.add(root + File.separator + CONTENT);
    list.add(root + File.separator + "MyTest" + File.separator + "MyTest2" + CONTENT);

    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("var1", "var1");
    map1.put("var2", "var2");
    Page page1 = new Page();
    page1.setVariables(map1);

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("var2", "qwe");
    map2.put("var3", "var3");
    Page page2 = new Page();
    page2.setVariables(map2);

    EasyMock.expect(loader.loadPage(list.get(0))).andReturn(page1);
    EasyMock.expect(loader.loadPage(list.get(1))).andReturn(page2);
    EasyMock.replay(loader);

    HashMap<String, String> parentVariables = creator.getParentVariables(list, root);
    EasyMock.verify(loader);

    assertEquals(3, parentVariables.size());
    assertEquals("var1", parentVariables.get("var1"));
    assertEquals("qwe", parentVariables.get("var2"));
    assertEquals("var3", parentVariables.get("var3"));
  }

  @Test
  public void loadPageWithoutParentVariablesTest() throws IOException {
    String root = "ROOT";
    String fileName = root + File.separator + "path1";
    String path2 = "path2";

    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("var1", "var1");
    map1.put("var2", "var2");
    Page page1 = new Page();
    page1.setVariables(map1);

    HashMap<String, String> map2 = new HashMap<String, String>();
    map2.put("var2", "qwe");
    map2.put("var3", "var3");
    Page page2 = new Page();
    page2.setVariables(map2);

    Include include = new Include(path2, page2);
    page1.insert(include);

    EasyMock.expect(loader.loadPage(fileName)).andReturn(page1);
    EasyMock.expect(loader.loadPage(PageUtils.getFullPath(root, path2))).andReturn(page2);
    EasyMock.replay(loader);

    Page page = creator.loadPageWithoutParentVariables(fileName, root);

    assertEquals(3, page.getVariables().size());
    assertEquals("var1", page.getVariables().get("var1"));
    assertEquals("qwe", page.getVariables().get("var2"));
    assertEquals("var3", page.getVariables().get("var3"));
  }

  @SuppressWarnings("serial")
  public class TestParentVariablesCreator extends ParentVariablesCreator {

      public TestParentVariablesCreator(PageLoader loader) {
          super(loader);
      }
  }
}
